﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ATM_Pro.Module;
using System.IO;
using ATM_Pro.Public;

namespace ATM_Pro
{
    public partial class OptionsForm : Form
    {
        public OptionsForm()
        {
            InitializeComponent();
            InitAtmList();
            InitGridView();
            InitSettings();
        }

        private void InitGridView()
        {
            this.gridAtms.AutoGenerateColumns = false;
            this.gridAtms.Columns[0].DataPropertyName = "AddName";
            this.gridAtms.Columns[1].DataPropertyName = "Ip";
            this.txtWarnTime.Text = Properties.Settings.Default.AlarmTimeInterval.ToString();
        }

        private void InitAtmList()
        {
            Configuration configuration = Configuration.GetInstance();
            listAtm.Clear();
            foreach (ATM item in configuration.Atms)
            {
                listAtm.Add(item);
            }
        }
       
        private string preDbPath = string.Empty;
        private void InitSettings()
        {
            Properties.Settings settings = Properties.Settings.Default;
            txtDbSavePath.Text = settings.DB_Path;
            txtFileSavePath.Text = settings.OutputPath;
            preDbPath = settings.DB_Path;
        }

        private BindingSource bs;
        BindingList<ATM> listAtm = new BindingList<ATM>(); 

        public List<ATM> Atms
        {
            get
            {
                List<ATM> atms = new List<ATM>();

                foreach (ATM item in listAtm)
                {
                    atms.Add(item);
                }
                return atms;
            }
            set
            {
                listAtm.Clear();
                foreach (ATM atm in value)
                {
                    listAtm.Add(atm);
                }
                bs = new BindingSource();
                bs.DataSource = listAtm;
                
                gridAtms.DataSource = bs;
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            string dbSavePath = txtDbSavePath.Text.Trim();
            string fileSavePath = txtFileSavePath.Text.Trim();
            if (string.IsNullOrEmpty(dbSavePath))
                MessageBox.Show("请选择数据库存放路径！");
            if (string.IsNullOrEmpty(fileSavePath))
                MessageBox.Show("请选择图片和文件存放路径！");
            
            if (!Directory.Exists(dbSavePath))
                Directory.CreateDirectory(dbSavePath);
            if (!Directory.Exists(fileSavePath))
                Directory.CreateDirectory(fileSavePath);

            try
            {
                if (preDbPath != dbSavePath)
                    File.Move(Path.Combine(preDbPath, FileHandle.dbName), Path.Combine(dbSavePath, FileHandle.dbName));

            }
            catch (System.Exception ex)
            {
                MessageBox.Show("没有找到数据库文件！");
                return;
            }
           
            Properties.Settings settings = Properties.Settings.Default;
            settings.AlarmTimeInterval = Convert.ToInt32(txtWarnTime.Text.Trim());
            settings.DB_Path = dbSavePath;
            settings.AccessConnectiongString = FileHandle.GeneratorDBConnectionString(dbSavePath);
            settings.OutputPath = fileSavePath;

            settings.Save();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
            this.Dispose();
        }

        private void btnBrowsDbFileSaveFolder_Click(object sender, EventArgs e)
        {
            txtDbSavePath.Text = BrowserFolder();
        }

        private string BrowserFolder()
        {
            FolderBrowserDialog folderBrowser = new FolderBrowserDialog();
            if (folderBrowser.ShowDialog() == DialogResult.OK)
            {
                return folderBrowser.SelectedPath;
            }
            return string.Empty;
        }

        private void btnBrowsFileSaveFolder_Click(object sender, EventArgs e)
        {
            txtFileSavePath.Text = BrowserFolder();
        }
    }
}
